#include "ele_type.h"
#include <cmath>

namespace PhysLeo {

int eleVertNumFromEleType(const EleType ele_type)
{
    if (ele_type == EleType::NONE)return 0;
    else if (ele_type == EleType::TRI)return 3;
    else if (ele_type == EleType::QUAD)return 4;
    else if (ele_type == EleType::TET)return 4;
    else if (ele_type == EleType::HEX)return 8;
    else return 0;
}

float volumeFromEleType(const EleType ele_type, std::vector<glm::tvec3<float>>& pos)
{
    if(ele_type==EleType::TRI)
    {
        return fabs(glm::length(glm::cross(pos[1] - pos[0], pos[2] - pos[0])) / 2.0f);
    }
    else if(ele_type==EleType::QUAD)
    {
        const auto s102 = fabs(glm::length(glm::cross(pos[0] - pos[1], pos[2] - pos[1])) / 2.0f);
        const auto s302 = fabs(glm::length(glm::cross(pos[0] - pos[3], pos[2] - pos[3])) / 2.0f);
        return s102 + s302;
    }
    else if(ele_type==EleType::TET)
    {
        return fabs(glm::dot(pos[3] - pos[0], glm::cross(pos[2] - pos[0], pos[1] - pos[0])) / 6.0f);
    }
    else if(ele_type==EleType::HEX)
    {
        const auto v0125 = fabs(glm::dot(pos[0] - pos[1], glm::cross(pos[2] - pos[1], pos[5] - pos[1]))) / 6.0f;
        const auto v0457 = fabs(glm::dot(pos[0] - pos[4], glm::cross(pos[5] - pos[4], pos[7] - pos[4]))) / 6.0f;
        const auto v6257 = fabs(glm::dot(pos[2] - pos[6], glm::cross(pos[5] - pos[6], pos[7] - pos[6]))) / 6.0f;
        const auto v3027 = fabs(glm::dot(pos[0] - pos[3], glm::cross(pos[2] - pos[3], pos[7] - pos[3]))) / 6.0f;
        const auto v0257 = fabs(glm::dot(pos[2] - pos[0], glm::cross(pos[5] - pos[0], pos[7] - pos[0]))) / 6.0f;
        return v0125 + v0457 + v6257 + v3027 + v0257;
    }
    else
    {
        return 0.0;
    }
}

}
